const cartModel = require('../db/cart')
const goodModel = require('../db/goods')
const { emit } = require('../db/users')
const returned = require('../error_returned/returned')

module.exports = {
  //添加购物车
  cartadd: async (req, res) => {
    let { userid, goodsid, goodsnumber } = req.body
    // console.log(userid, goodsid, goodsnumber);
    if (userid === undefined) {
      console.log(222);
      returned(res, 1, '添加失败')

      return
    }
    if (goodsid === undefined) {
      console.log(111);
      returned(res, 1, '添加失败')

      return
    }
    // returned(res, 0, '成功', {
    //   userid, goodsid, goodsnumber
    // })
    //查看数据库中是否有该用户购物车的信息
    let cartData = await cartModel.findOne({ userid, goodsid })
    if (cartData) {
      // 有就修改
      // console.log(cartData);
      await cartModel.updateOne({ userid, goodsid }, { goodsnumber: cartData.goodsnumber + 1 })
      returned(res, 0, '添加成功')
    } else {
      //没有就添加
      let docs = await cartModel.insertMany([{
        userid,
        goodsid,
        goodsnumber
      }])
      if (docs.length === 0) {
        returned(res, 1, '添加失败')
        return
      }
      returned(res, 0, '添加成功')
    }
  },
  //删除
  cartremoveOne: async (req, res) => {
    let { userid, goodsid } = req.body
    //查看数据库中是否有该用户购物车的信息
    let cartData = await cartModel.findOne({ userid, goodsid })
    //购物车数量小于对于0删除
    if (cartData.goodsnumber <= 0) {
      await cartModel.deleteOne({ userid, goodsid })
    }
    if (cartData) {
      // 有就把这个表的信息全部删除
      // console.log(cartData);
      await cartModel.updateOne({ userid, goodsid }, { goodsnumber: cartData.goodsnumber - 1 })
      // await cartModel.deleteOne({ userid, goodsid })
      returned(res, 0, '删除成功')
    } else {
      returned(res, 1, '无该购物车信息')
    }
  },
  //列表
  cartgoods: async (req, res) => {
    let { id } = req.params
    //查找购物车表该用户的所有商品信息
    let docs = await cartModel.find({ userid: id })
    if (!docs) {
      returned(res, 1, '无购物车信息')
      return
    }
    let data = []
    let newdata = []
    docs.forEach(element => {
      data.push(element.goodsid)
    })
    let newdocs = await goodModel.find({ _id: data })
    for (let i = 0; i < newdocs.length; i++) {
      let list = newdocs[i]
      let quantity = docs[i].goodsnumber
      newdata.push({
        list,
        quantity
      })
    }
    returned(res, 0, '成功', newdata)
  },
  //清除所有
  empty: async (req, res) => {
    let { id: userid } = req.params
    //查看数据库中是否有该用户购物车的信息
    let cartData = await cartModel.find({ userid })
    // 删除该用户名下的所有购物车
    console.log(cartData);
    if (!cartData.length) {
      returned(res, 1, '删除失败')
      return
    }
    await cartModel.remove({ userid })
    returned(res, 0, '删除成功')
  },
  //根据传的值修改
  changenumber: async (req, res) => {
    let { userid, goodsid, goodsnumber } = req.body
    //查看数据库中是否有该用户购物车的信息
    let cartData = await cartModel.findOne({ userid, goodsid })
    if (cartData) {
      // console.log(cartData);
      await cartModel.updateOne({ userid, goodsid }, { goodsnumber })
      // await cartModel.deleteOne({ userid, goodsid })
      returned(res, 0, '修改成功')
    } else {
      returned(res, 1, '无该购物车信息')
      return
    }
  }
}